package com.ruoyi.enterwarehouse.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.enterwarehouse.domain.TGoodsEnterImport;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseImport;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.domain.TGoodsWeightSum;
import com.ruoyi.goods.service.impl.TGoodsInfoServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.enterwarehouse.mapper.WEnterWarehouseInfoMapper;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseInfo;
import com.ruoyi.enterwarehouse.service.IWEnterWarehouseInfoService;

/**
 * 入库信息Service业务层处理
 * 
 * @author wzy
 * @date 2024-09-02 10:44:55
 */
@Service
public class WEnterWarehouseInfoServiceImpl implements IWEnterWarehouseInfoService 
{
    @Autowired
    private WEnterWarehouseInfoMapper wEnterWarehouseInfoMapper;

    private static Logger logger = LoggerFactory.getLogger(WEnterWarehouseInfoServiceImpl.class);
    /**
     * 查询入库信息
     * 
     * @param enterWarehouseId 入库信息主键
     * @return 入库信息
     */
    @Override
    public WEnterWarehouseInfo selectWEnterWarehouseInfoByEnterWarehouseId(Long enterWarehouseId)
    {
        return wEnterWarehouseInfoMapper.selectWEnterWarehouseInfoByEnterWarehouseId(enterWarehouseId);
    }

    /**
     * 查询入库信息列表
     * 
     * @param wEnterWarehouseInfo 入库信息
     * @return 入库信息
     */
    @Override
    public List<WEnterWarehouseInfo> selectWEnterWarehouseInfoList(WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        return wEnterWarehouseInfoMapper.selectWEnterWarehouseInfoList(wEnterWarehouseInfo);
    }

    /**
     * 新增入库信息
     * 
     * @param wEnterWarehouseInfo 入库信息
     * @return 结果
     */
    @Override
    public int insertWEnterWarehouseInfo(WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        wEnterWarehouseInfo.setCreateTime(DateUtils.getNowDate());
        return wEnterWarehouseInfoMapper.insertWEnterWarehouseInfo(wEnterWarehouseInfo);
    }

    /**
     * 修改入库信息
     * 
     * @param wEnterWarehouseInfo 入库信息
     * @return 结果
     */
    @Override
    public int updateWEnterWarehouseInfo(WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        wEnterWarehouseInfo.setUpdateTime(DateUtils.getNowDate());
        return wEnterWarehouseInfoMapper.updateWEnterWarehouseInfo(wEnterWarehouseInfo);
    }

    /**
     * 批量删除入库信息
     * 
     * @param enterWarehouseIds 需要删除的入库信息主键
     * @return 结果
     */
    @Override
    public int deleteWEnterWarehouseInfoByEnterWarehouseIds(Long[] enterWarehouseIds)
    {
        return wEnterWarehouseInfoMapper.deleteWEnterWarehouseInfoByEnterWarehouseIds(enterWarehouseIds);
    }

    /**
     * 删除入库信息信息
     * 
     * @param enterWarehouseId 入库信息主键
     * @return 结果
     */
    @Override
    public int deleteWEnterWarehouseInfoByEnterWarehouseId(Long enterWarehouseId)
    {
        return wEnterWarehouseInfoMapper.deleteWEnterWarehouseInfoByEnterWarehouseId(enterWarehouseId);
    }

    /**
     * 导入货物数据
     *
     * @param enterList 用户数据列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importEnterTask(List<WEnterWarehouseImport> enterList, String operName)
    {
        if (StringUtils.isNull(enterList) || enterList.size() == 0)
        {
            throw new ServiceException("导入货物数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        Date currentDate = new Date();

        for(WEnterWarehouseImport enter : enterList)
        {
            try
            {
              enter.setEnterWarehouseNumber(OrderUtils.getOrderNumber("EN", "-"));
//              enter.setRemark("导入创建入库单，捆包号与入库单号同");
              enter.setJobStatus("2");//作业

//              enter.setEnterWarehouseTime(DateUtils.getNowDate());
              enter.setEnterNumberOfPiece(1);
              enter.setEnterWarehousePerson(operName);
              enter.setCreateBy(operName);
              enter.setCreateTime(DateUtils.getNowDate());

              enter.setBundleNumber(enter.getEnterWarehouseNumber());
              enter.setGoodsRightCustomer(enter.getEnterWarehouseCustomer());
              enter.setRecordSource("PC");
              enter.setInventoryStatus("入库中");
              enter.setWarehouseType("室外库");
              enter.setWarehouseTotalLocate(enter.getLogisticsParkName()+enter.getWarehouseLocateName()+enter.getRegionLocateName());
              if(wEnterWarehouseInfoMapper.insertWEnterWarehouseInfoExt(enter)>0)
              {
                  successNum++;
                  successMsg.append("<br/>" + successNum + "、入库导入 " + enter.getEnterWarehouseNumber() + " 导入成功");
              }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、入库作业 " + enter.getEnterWarehouseNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                logger.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改入库信息
     *
     * @param enterWarehouseNumber 入库信息
     * @return 结果
     */
    @Override
    public int updateWEnterTotalWeightPieceByNumber(String enterWarehouseNumber)
    {

        return wEnterWarehouseInfoMapper.updateWEnterTotalWeightPieceByNumber(enterWarehouseNumber);
    }

    /**
     * 获取货物重量合计
     *
     * @param wEnterWarehouseInfo 货物信息参数
     * @return 结果
     */
    @Override
    public List<TGoodsWeightSum> getWeightSumByEnterWarehouseInfo(WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        return wEnterWarehouseInfoMapper.getWeightSumByEnterWarehouseInfo(wEnterWarehouseInfo);
    }

    /**
     * 查询入库信息
     *
     * @param enterWarehouseNumber 入库信息主键
     * @return 入库信息
     */
    @Override
    public WEnterWarehouseInfo selectWEnterWarehouseInfoByEnterWarehouseNumber(String enterWarehouseNumber)
    {
        return wEnterWarehouseInfoMapper.selectWEnterWarehouseInfoByEnterWarehouseNumber(enterWarehouseNumber);
    }

    /**
     * 查询入库信息，最大入库ID
     *
     * @return 入库ID
     */
    @Override
    public Long selectEnterWarehouseInfoMaxEnterWarehouseId()
    {
        return wEnterWarehouseInfoMapper.selectEnterWarehouseInfoMaxEnterWarehouseId();
    }

}
